#include <iostream>
#include <sstream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>

// 实现数据的序列化
/*bool serialize(const Json::Value &val,  std::string &body) {
    std::stringstream ss;
    //先实例化一个工厂类对象
    Json::StreamWriterBuilder swb;
    //通过工厂类对象来生产派生类对象
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    int ret = sw->write(val, &ss);
    if (ret != 0) {
        std::cout << "json serialize failed!\n";
        return false;
    }
    body = ss.str();
    return true;
}*/

// 实现json字符串的反序列化
/*bool unserialize(const std::string &body, Json::Value &val) {
    //实例化工厂类对象
    Json::CharReaderBuilder crb;
    //生产CharReader对象
    std::string errs;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
    bool ret = cr->parse(body.c_str(), body.c_str() + body.size(), &val, &errs);
    if (ret == false) {
        std::cout << "json unserialize failed : " << errs << std::endl;
        return false;
    }
    return true;
}*/

int main()
{
    const char* name = "小明";
    int age = 18;
    std::string telephone = "123757558";

    Json::Value root;
    root["姓名"] = name;
    root["年龄"] = age;
    root["电话号码"] = telephone;

    // 检查是否为布尔值
    if (root["is_student"].isBool()) {
        bool isStudent = root["is_student"].asBool();
        std::cout << "is_student: " << isStudent << std::endl;
    } else {
        std::cerr << "Error: is_student is not a boolean value." << std::endl;
    }

    // 使用StreamWriterBuilder来写入JSON数据
    Json::StreamWriterBuilder stm;
    stm["commentStyle"] = "None";
    stm["indentation"] = "   ";
    std::unique_ptr<Json::StreamWriter> ptr(stm.newStreamWriter());
    std::stringstream ss;
    ptr->write(root, &ss);
    std::string body = ss.str();
    std::cout << body.c_str() << std::endl;

    return 0;
}